/*
 * PsApplication.cpp
 *
 *  Created on: 2016年4月6日
 *      Author: terry
 */

#include "PsApplication.h"
#include "Socket.h"
#include "CLog.h"
#include "Ffmpeg.h"

typedef comn::Singleton< PsApplication >		PsApplicationSingleton;


int getCLogLevel(int level)
{
	if (level <= AV_LOG_FATAL)
	{
		return CLog::kFatal;
	}
	else if (level <= AV_LOG_ERROR)
	{
		return CLog::kError;
	}
	else if (level <= AV_LOG_WARNING)
	{
		return CLog::kWarning;
	}
	else if (level <= AV_LOG_INFO)
	{
		return CLog::kInfo;
	}
	else
	{
		return CLog::kDebug;
	}
}

void av_log_callback(void *avcl, int level, const char *fmt, va_list vl)
{
	//av_log_default_callback(avcl, level, fmt, vl);

	if (level <= AV_LOG_WARNING)
	{
		CLog::logv(getCLogLevel(level), fmt, vl);
	}
}

PsApplication& PsApplication::instance()
{
	return PsApplicationSingleton::instance();
}

PsApplication::PsApplication()
{
}

PsApplication::~PsApplication()
{
}


int PsApplication::init()
{
	if (0 != CLog::startup("PsCasterLog.ini"))
	{
		CLog::setLogger(CLog::DEBUGWINDOW);
		CLog::setLogger(CLog::COUT);
	}

	comn::Socket::startup();

	return 0;
}

void PsApplication::quit()
{
	m_casters.clear();

	comn::Socket::cleanup();
}


int PsApplication::put(PsMediaCasterPtr& caster)
{
	if (!caster)
	{
		return -1;
	}

	return m_casters.put(caster);
}

PsMediaCasterPtr PsApplication::get(int idx)
{
	if (idx < 0 || idx >= (int)m_casters.max_size())
	{
		return PsMediaCasterPtr();
	}
	return m_casters.getAt(idx);
}

PsMediaCasterPtr PsApplication::remove(int idx)
{
	PsMediaCasterPtr caster;
	m_casters.remove(idx, caster);
	return caster;
}

